package com.haochang.chunkvod.controller.fragment.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshBase;
import com.haochang.chunkvod.view.thirdparty.PullToRefresh.PullToRefreshListView;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * 咨客--预留房间--预留房间列表
 *     @author Yangjie
 *     className Fragment_reserve_room_list
 *     created at  2016/11/1  17:56
 */
public class ReserveRoomListFragment extends Fragment implements View.OnClickListener
{
	private Context mContext;
	private View view;// infalte的布局
	private LinearLayout myContainer;// 新建容器
	////////////////////////////////
	private PullToRefreshListView listView;
	private HostessData hostessData;
	private QuickAdapter<HostessInfo> adapter;
	private List<HostessInfo> hostessInfoList = new ArrayList<HostessInfo>();

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		mContext = getActivity();
		hostessData = new HostessData((BaseActivity) mContext);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		if (null == view)
		{
			myContainer = new LinearLayout(mContext);
			view = inflater.inflate(R.layout.fragment_reserve_room_list, container, false);
			view.setMinimumHeight(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight());
			view.setMinimumWidth(((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth());
			myContainer.addView(view);
		} else
		{
			myContainer.removeAllViews();
			myContainer = new LinearLayout(getActivity());
			myContainer.addView(view);
		}
		initView();
		initData();
		return myContainer;
	}

	@Override
	public void onResume() {
		super.onResume();
		requestReserveRoomList();//获取预留房间列表
	}

	private void initData()
	{
		adapter = new QuickAdapter<HostessInfo>(mContext, R.layout.item_reserve_room, hostessInfoList)
		{
			@Override
			protected void convert(BaseAdapterHelper helper, HostessInfo item)
			{
				helper.setText(R.id.tv_room_num, item.getRoomNum());
				helper.setText(R.id.tv_date, item.getStart_time());
				helper.setText(R.id.tv_nick, item.getAreaName());//顾客nick，用getAreaName代替
				helper.setText(R.id.tv_reserver, item.getName());
			}
		};
		listView.setAdapter(adapter);
		listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<BaseListView>()
		{
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{
				requestReserveRoomList();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<BaseListView> refreshView)
			{

			}
		});

	}

	private void initView()
	{
		listView = (PullToRefreshListView) view.findViewById(R.id.listView);
	}

	/**
	 * 获取预约房间list
	 */
	@SuppressWarnings("unchecked")
	private void requestReserveRoomList()
	{
		hostessData.requestReserveRoomList("", new HostessData.ResultResponseList()
		{
			@Override
			public void onRequestSuccess(List list)
			{
				listView.onRefreshComplete();
				if (!CollectionUtils.isEmpty(list))
				{
					hostessInfoList.clear();
					hostessInfoList.addAll(list);
					adapter.replaceAll(list);
				}
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
				listView.onRefreshComplete();
				ToastUtils.showText(mContext, TextUtils.isEmpty(errMsg) ? "获取预留房间列表失败!" : errMsg);
			}
		});
	}

	@Override
	public void onClick(View view)
	{
		switch (view.getId())
		{

		default:
			break;
		}
	}

}
